package com.talkyun.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JarVersionHelper {
    private static String version = null;

    public static String getJarVersion(String groupId, String artifactId) {
        if (version == null) {
            try {
                version = readOpenxVersion(groupId, artifactId);
            } catch (Exception e) {
                version = "error";
            }
        }
        return version;
    }

    private static String readOpenxVersion(String groupId, String artifactId) throws IOException {
        String path = "/META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties";

        System.out.println(path);
        System.out.println(JarVersionHelper.class.getResourceAsStream(path));

        InputStream ins = JarVersionHelper.class.getResourceAsStream(path);


        Properties p = new Properties();
        p.load(ins);
        return p.getProperty("version");
    }

    public static void main(String[] args) {

        String ver = JarVersionHelper.getJarVersion("com.alibaba", "fastjson");

        System.out.println(ver);
    }
}